<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="rolePage">
        <el-row>
          <el-col :sm="4">
            <div class="g-title">早会简报</div>
          </el-col>
          <el-col :sm="20" style="text-align: right">
            <span class="role-add" @click="handleCreate()"><i class="el-icon-plus" />添加</span>
          </el-col>
        </el-row>
        <el-date-picker
          v-model="singleTime"
          type="date"
          placeholder="选择早会日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          class="filter-item"
          @change="fetchData()"
        />

      </div>
    </div>
    <el-row class="table">
      <el-table
        ref="multipleTable"
        :data="list"
        tooltip-effect="dark"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="workLines" label="线别" show-overflow-tooltip align="center" />
        <el-table-column prop="customerName" label="客户名称" show-overflow-tooltip align="center" />
        <el-table-column prop="productPatterns" label="版型" show-overflow-tooltip align="center" />
        <el-table-column prop="plannedInputs" label="计划投入" show-overflow-tooltip align="center" />
        <el-table-column prop="actualInputs" label="实际投入" show-overflow-tooltip align="center" />
        <el-table-column prop="plannedAchiRates" label="计划达成率" show-overflow-tooltip align="center" />
        <el-table-column prop="actualInStorage" label="实际入库" show-overflow-tooltip align="center" />
        <el-table-column prop="wipProducts" label="WIP" show-overflow-tooltip align="center" />
        <el-table-column prop="goodRates" label="良率" show-overflow-tooltip align="center" />
        <el-table-column prop="workLossRates" label="工损" show-overflow-tooltip align="center" />
        <el-table-column prop="originalLossRates" label="原损" show-overflow-tooltip align="center" />
        <el-table-column prop="chuanReturnRates" label="串返" show-overflow-tooltip align="center" />
        <el-table-column prop="dieReturnRates" label="叠返" show-overflow-tooltip align="center" />
        <el-table-column prop="remarks" label="重点事项" show-overflow-tooltip align="center">
          <template slot-scope="{row,$index}">
            <el-link type="primary" @click="SeeRow(row)">查看</el-link>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="240">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
              修改
            </el-button>
            <el-button v-if="row.status != 'deleted'" size="mini" type="danger" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination">
        <el-col>
          <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :page-size="pageSize"
            :total="total"
            :current-page="pageNo"
            @current-change="fetchData"
            @size-change="pageSizeChange"
          />
        </el-col>
      </el-row>
    </el-row>
    <!-- 查看详情弹窗 -->
    <el-dialog title="查看" :visible.sync="dialogSee">
      <el-form
        ref="SeeForm"
        :model="SeeData"
        label-position="left"
        label-width="80px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="投入达成">
          <span>{{ SeeData.inputFinish }}</span>
        </el-form-item>
        <el-form-item label="碎片">
          <span>{{ SeeData.remarkSuiPian }}</span>
        </el-form-item>
        <el-form-item label="良率">
          <span>{{ SeeData.remarkGoodRates }}</span>
        </el-form-item>
        <el-form-item label="其他">
          <span>{{ SeeData.remarkOther }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSee = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
    <!-- 添加修改弹窗 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item v-show="dialogStatus === 'create'" label="早会日期" prop="meetingDate">
          <el-date-picker
            v-model="temp.meetingDate"
            type="date"
            placeholder="选择早会日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            @change="handleDateChange()"
          />
        </el-form-item>
        <el-form-item label="线别" prop="workLines">
          <el-input v-model="temp.workLines" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="temp.customerName" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
        <el-form-item label="版型" prop="productPatterns">
          <el-input v-model="temp.productPatterns" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
        <el-form-item label="计划投入" prop="plannedInputs">
          <el-input
            v-model="temp.plannedInputs"
            placeholder="请输入内容"
            clearable
            :disabled="temp.disabled"
            @change="handleInputChange"
          />
        </el-form-item>
        <el-form-item label="实际投入" prop="actualInputs">
          <el-input
            v-model="temp.actualInputs"
            placeholder="请输入内容"
            clearable
            :disabled="temp.disabled"
            @change="handleInputChange"
          />
        </el-form-item>
        <el-form-item label="计划达成率" prop="plannedAchiRates">
          <el-input v-model="temp.plannedAchiRates" placeholder="请输入计划投入和实际投入" clearable :disabled="true" />
        </el-form-item>
        <el-form-item label="实际入库" prop="actualInStorage">
          <el-input v-model="temp.actualInStorage" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
        <el-form-item label="WIP" prop="wipProducts">
          <el-input v-model="temp.wipProducts" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
        <el-form-item label="良率" prop="goodRates">
          <el-input v-model="temp.goodRates" placeholder="请输入内容" clearable :disabled="temp.disabled">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="工损" prop="workLossRates">
          <el-input v-model="temp.workLossRates" placeholder="请输入内容" clearable :disabled="temp.disabled">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="原损" prop="originalLossRates">
          <el-input v-model="temp.originalLossRates" placeholder="请输入内容" clearable :disabled="temp.disabled">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="串返" prop="chuanReturnRates">
          <el-input v-model="temp.chuanReturnRates" placeholder="请输入内容" clearable :disabled="temp.disabled">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item label="叠返" prop="dieReturnRates">
          <el-input v-model="temp.dieReturnRates" placeholder="请输入内容" clearable :disabled="temp.disabled">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-row>
          <el-col :sm="8">
            <div class="title">重点事项</div>
          </el-col>
        </el-row>
        <el-form-item label="投入达成" prop="inputFinish">
          <el-input v-model="temp.inputFinish" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
        <el-form-item label="碎片" prop="remarkSuiPian">
          <el-input v-model="temp.remarkSuiPian" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
        <el-form-item label="良率" prop="remarkOther">
          <el-input v-model="temp.remarkGoodRates" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
        <el-form-item label="其他" prop="remarks">
          <el-input v-model="temp.remarkOther" placeholder="请输入内容" clearable :disabled="temp.disabled" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          {{ textMap[dialogStatus] }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import './css/index.scss'
import { briefAdd, briefAllItems, judgeAllItems, briefDelete, briefUpdate } from '@/api/report'
export default {
  data: function() {
    return {
      activeCardName: 'all',
      dialogFormVisible: false,
      dialogSee: false,
      dialogStatus: '',
      // 查询
      singleTime: '',
      langtopicTypeData: [],
      textMap: {
        update: '修改',
        create: '添加'
      },
      SeeData: {
        inputFinish: '',
        remarkSuiPian: '',
        remarkGoodRates: '',
        remarkOther: ''
      },
      temp: {
        meetingId: '',
        workLines: '',
        customerName: '',
        productPatterns: '',
        plannedInputs: '',
        actualInputs: '',
        actualInStorage: '',
        wipProducts: '',
        goodRates: '',
        workLossRates: '',
        originalLossRates: '',
        chuanReturnRates: '',
        dieReturnRates: '',
        inputFinish: '',
        remarkSuiPian: '',
        remarkGoodRates: '',
        remarkOther: '',
        disabled: false
      },
      rules: {
        meetingId: [{ required: true, message: '早会日期没有选择', trigger: 'change' }],
        workLines: [{ required: true, message: '线别没有填写', trigger: 'blur' }],
        customerName: [{ required: true, message: '没有填写', trigger: 'blur' }],
        productPatterns: [{ required: true, message: '没有填写', trigger: 'blur' }],
        plannedInputs: [{ required: true, message: '没有填写', trigger: 'change' }],
        actualInputs: [{ required: true, message: '没有填写', trigger: 'change' }],
        actualInStorage: [{ required: true, message: '没有填写', trigger: 'blur' }],
        wipProducts: [{ required: true, message: '没有填写', trigger: 'blur' }],
        goodRates: [{ required: true, message: '没有填写', trigger: 'blur' }],
        workLossRates: [{ required: true, message: '没有填写', trigger: 'blur' }],
        originalLossRates: [{ required: true, message: '没有填写', trigger: 'blur' }],
        chuanReturnRates: [{ required: true, message: '没有填写', trigger: 'blur' }],
        dieReturnRates: [{ required: true, message: '没有填写', trigger: 'blur' }]
      },
      total: 0,
      pageNo: 1,
      pageSize: 10,
      list: [], // 全部数据
      listDataSelected: [] // 选中数据
    }
  },
  created() {
    // 获取数据
    // this.fetchData();
  },
  methods: {
    // 重置数据
    resetTemp() {
      this.temp = {
        meetingId: '',
        workLines: '',
        customerName: '',
        productPatterns: '',
        plannedInputs: '',
        actualInputs: '',
        actualInStorage: '',
        wipProducts: '',
        goodRates: '',
        workLossRates: '',
        originalLossRates: '',
        chuanReturnRates: '',
        dieReturnRates: '',
        inputFinish: '',
        remarkSuiPian: '',
        remarkGoodRates: '',
        remarkOther: '',
        disabled: true
      }
    },
    // 日期选择
    async handleDateChange() {
      await judgeAllItems({ meetingDate: this.temp.meetingDate }).then((r) => {
        if (r && r.code == 200) {
          this.temp.disabled = false
          this.$message({
            type: 'success',
            message: r.message
          })
          this.temp.meetingId = r.data[0].meetingId
        }
      }).catch((e) => {
        this.$message({
          type: 'error',
          message: '早会不存在，请先添加早会！'
        })
      }).finally((e) => {
        console.log('finally', e)
      })
    },
    // 计算计划达成率
    handleInputChange() {
      if (this.temp.plannedInputs && this.temp.actualInputs) {
        this.temp.plannedAchiRates = ((this.temp.actualInputs / this.temp.plannedInputs) * 100).toFixed(2) + '%'
        this.$forceUpdate()
      }
    },
    // 打开添加早会弹窗
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    //  修改早会
    handleUpdate(row) {
      const data = Object.assign({}, row)
      this.temp = {
        singleTime: this.singleTime,
        briefId: data.briefId,
        meetingId: data.meetingId,
        workLines: data.workLines,
        customerName: data.customerName,
        productPatterns: data.productPatterns,
        plannedAchiRates: data.plannedAchiRates,
        plannedInputs: data.plannedInputs,
        actualInputs: data.actualInputs,
        actualInStorage: data.actualInStorage,
        wipProducts: data.wipProducts,
        goodRates: data.goodRates,
        workLossRates: data.workLossRates,
        originalLossRates: data.originalLossRates,
        chuanReturnRates: data.chuanReturnRates,
        dieReturnRates: data.dieReturnRates,
        inputFinish: data.inputFinish,
        remarkSuiPian: data.remarkSuiPian,
        remarkGoodRates: data.remarkGoodRates,
        remarkOther: data.remarkOther,
        disabled: true
      }
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 添加早会提交数据
    createData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const data = {
            meetingId: this.temp.meetingId,
            workLines: this.temp.workLines,
            customerName: this.temp.customerName,
            productPatterns: this.temp.productPatterns,
            plannedAchiRates: this.temp.plannedAchiRates,
            plannedInputs: this.temp.plannedInputs,
            actualInputs: this.temp.actualInputs,
            actualInStorage: this.temp.actualInStorage,
            wipProducts: this.temp.wipProducts,
            // %
            goodRates: this.temp.goodRates + '%',
            workLossRates: this.temp.workLossRates + '%',
            originalLossRates: this.temp.originalLossRates + '%',
            chuanReturnRates: this.temp.chuanReturnRates + '%',
            dieReturnRates: this.temp.dieReturnRates + '%',

            inputFinish: this.temp.inputFinish,
            remarkSuiPian: this.temp.remarkSuiPian,
            remarkGoodRates: this.temp.remarkGoodRates,
            remarkOther: this.temp.remarkOther
          }
          await briefAdd(data).then((r) => {
            this.dialogFormVisible = false
            this.singleTime = this.temp.singleTime
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message
              })
            }
          })
        }
      })
    },
    // 修改早会提交数据
    updateData() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (valid) {
          const data = {
            briefId: this.temp.briefId,
            meetingId: this.temp.meetingId,
            workLines: this.temp.workLines,
            customerName: this.temp.customerName,
            productPatterns: this.temp.productPatterns,
            plannedAchiRates: this.temp.plannedAchiRates,
            plannedInputs: this.temp.plannedInputs,
            actualInputs: this.temp.actualInputs,
            actualInStorage: this.temp.actualInStorage,
            wipProducts: this.temp.wipProducts,
            // %
            goodRates: this.temp.goodRates + '%',
            workLossRates: this.temp.workLossRates + '%',
            originalLossRates: this.temp.originalLossRates + '%',
            chuanReturnRates: this.temp.chuanReturnRates + '%',
            dieReturnRates: this.temp.dieReturnRates + '%',

            inputFinish: this.temp.inputFinish,
            remarkSuiPian: this.temp.remarkSuiPian,
            remarkGoodRates: this.temp.remarkGoodRates,
            remarkOther: this.temp.remarkOther
          }
          await briefUpdate(data).then((r) => {
            this.singleTime = this.temp.singleTime
            this.dialogFormVisible = false
            if (r && r.code == 200) {
              this.$message({
                type: 'success',
                message: r.message
              })
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: r.message
              })
            }
          })
        }
      })
    },
    // 删除早会
    async handleDelete(row) {
      await briefDelete({ briefId: row.briefId }).then((r) => {
        if (r && r.code == 200) {
          this.$message({
            type: 'success',
            message: r.message
          })
          this.fetchData()
        } else {
          this.$message({
            type: 'error',
            message: r.message
          })
        }
      })
    },
    //  查看账户
    SeeRow(data) {
      this.SeeData = {
        inputFinish: data.inputFinish,
        remarkSuiPian: data.remarkSuiPian,
        remarkGoodRates: data.remarkGoodRates,
        remarkOther: data.remarkOther
      }
      this.dialogSee = true
    },
    // 分页组件
    pageSizeChange(newSize) {
      this.pageSize = newSize
      this.pageNo = 1
      this.fetchData()
    },
    delRole(data) {
      this.$confirm('此操作将永久删除账户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          console.log('data', data)
          const r = await deleteUser({ userId: data.userId })
          if (r && r.code == 200) {
            this.$message({
              type: 'success',
              message: r.message
            })
            this.fetchData()
          } else {
            this.$message({
              type: 'error',
              message: r.message
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async fetchData(pageNo) {
      if (pageNo) {
        this.pageNo = pageNo
      }
      const params = {
        pageNum: this.pageNo,
        pageSize: this.pageSize,

        singleTime: ''
      }
      if (this.singleTime) {
        params.singleTime = this.singleTime
      }
      const data = await briefAllItems(params)
      if (data.data.content.length > 0) {
        this.$message({
          type: 'success',
          message: data.message
        })
        this.list = data.data.content
        this.total = data.data.totalSize
      } else {
        this.$message({
          type: 'error',
          message: '当前日期没有简报内容'
        })
        this.list = []
        this.total = 0
      }
    },
    // 表格多选
    handleSelectionChange(val) {
      this.listDataSelected.splice(0)
      val.forEach(e => {
        this.listDataSelected.push(e)
      })
    },
    getTime(time) {
      var date = new Date(time)
      var y = date.getFullYear()
      var m = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      return y + '-' + m + '-' + d
    }
  }
}
</script>
<style lang="scss" scoped>
.userlist {
  margin-right: 10px;
}

.title {
  height: 20px;
  padding-left: 5px;
  line-height: 20px;
  font-size: 20px;
  font-weight: bold;
  border-left: 7px solid #1587F9;
  margin-bottom: 23px;
  display: inline-block;
}
</style>
